#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Library.Entity;
using Library.Services;
using System.Collections;

#endregion

namespace Library
{
    public partial class UserControlBookCategory : UserControl
    {
        BookCategoryService m_BookCategoryService = new BookCategoryService();
        User m_currentUser;
        public UserControlBookCategory(string name, User currentUser, MainForm mainForm)
        {
            InitializeComponent();
            this.Name = name;
            this.Text = name;
            labelTitle.Text = name;
            m_currentUser = currentUser; 
            initColumn();
            initData();
            initButtonClick();
            toolBarButtonDelete.Enabled = listView.SelectedItem != null;
        }

        private void initButtonClick()
        {
            toolBarButtonAddNew.Click += new EventHandler(toolBarButtonAddNew_Click);
            toolBarButtonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
        }

        void toolBarButtonDelete_Click(object sender, EventArgs e)
        {
            if (listView.SelectedItem != null)
            {
                MessageBox.Show("Are you sure to delete?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, new EventHandler(DeleteData));
            }
        }

        void DeleteData(object sender, EventArgs e) 
        {
            try
            {
                MessageBoxWindow msg = (MessageBoxWindow)sender;
                if (msg.DialogResult == DialogResult.Yes)
                {
                    BookCategory BookCategory = (BookCategory)listView.SelectedItem.Tag;
                    if (BookCategory.Id > 0)
                    {
                        m_BookCategoryService.DeleteBookCategory(BookCategory);
                        MessageBox.Show("Record has been deleted.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                        listView.Items.Remove(listView.SelectedItem);
                    }
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void toolBarButtonAddNew_Click(object sender, EventArgs e)
        {
            FormBookCategory frm = new FormBookCategory(new BookCategory(), m_currentUser, this);
            frm.ShowDialog();
        }
       
        private void initColumn()
        {
            listView.Columns.Add("Code", 75, HorizontalAlignment.Left);
            listView.Columns.Add("Name", 100, HorizontalAlignment.Left);
            listView.Columns.Add("Remarks", 200, HorizontalAlignment.Left);
        }

        private void initData()
        {
            IList data = m_BookCategoryService.GetAllBookCategory();
            foreach(BookCategory BookCategory in data)
            {
                ListViewItem item = listView.Items.Add(BookCategory.Code);
                item.SubItems.Add(BookCategory.Name);
                item.SubItems.Add(BookCategory.Remark);
                item.Tag = BookCategory;
            }
        }

        internal void AddNewData(BookCategory BookCategory)
        {
            ListViewItem item = listView.Items.Add(BookCategory.Code);
            item.SubItems.Add(BookCategory.Name);
            item.SubItems.Add(BookCategory.Remark);
            item.Tag = BookCategory;
        }

        private void listView_DoubleClick(object sender, EventArgs e)
        {
            if (listView.SelectedItem != null)
            {
                BookCategory BookCategory = (BookCategory)listView.SelectedItem.Tag;
                FormBookCategory frm = new FormBookCategory(BookCategory, m_currentUser, this);
                frm.ShowDialog();
            }
        }

        internal void UpdateData(BookCategory BookCategory)
        {
            listView.SelectedItem.SubItems[0].Text = BookCategory.Code;
            listView.SelectedItem.SubItems[1].Text = BookCategory.Name;
            listView.SelectedItem.SubItems[2].Text = BookCategory.Remark;
            listView.SelectedItem.Tag = BookCategory;
        }

        private void listView_SelectedIndexChanged(object sender, EventArgs e)
        {
            toolBarButtonDelete.Enabled = listView.SelectedItem != null;
        }
    }
}